package com.atguigu.srb.sms.receiver;

import com.atguigu.srb.dto.SmsDTO;
import com.atguigu.srb.rabbitutil.constant.MQConst;
import com.atguigu.srb.sms.service.SmsService;
import com.atguigu.srb.sms.util.SmsProperties;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: srb
 * @author: 龙龙
 * @create: 2022-02-28 16:30
 * @description:
 **/
@Component
@Slf4j
public class SnsReceiver {
    @Autowired
    private SmsService smsService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MQConst.EXCHANGE_TOPIC_SMS, durable = "true"),
            key = {MQConst.ROUTING_SMS_ITEM},
            value = @Queue(value = MQConst.QUEUE_SMS_ITEM, durable = "true")
    ))
    public void sendListener(Channel channel, Message message, SmsDTO smsDTO) throws IOException {
        System.out.println(smsDTO);
        System.out.println("调用sms业务层发送消息");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        Map<String, Object> param = new HashMap<>();
        param.put("code", smsDTO.getMessage());
        smsService.send(smsDTO.getMobile(), SmsProperties.TEMPLATE_CODE, param);
    }
}
